﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Web.UI;

namespace AllNations.Framework
{
    public static class EditorHelper
    {
        public static void InitializeEditor(Telerik.Web.UI.RadEditor editor)
        {
            InitializeEditor(editor, "~/PageImages/Generic");
        }

        public static void InitializeEditor(Telerik.Web.UI.RadEditor editor, params string[] aMediaPaths)
        {
            Array.ForEach(aMediaPaths, path =>
            {
                string sPhysicalPath = HttpContext.Current.Server.MapPath(path);
                if (!System.IO.Directory.Exists(sPhysicalPath))
                    System.IO.Directory.CreateDirectory(sPhysicalPath);
            });
            bool IsAdministrator = HttpContext.Current.User.IsInRole("Administrator");
            int iMaxFileSize = 2097152;

            editor.Modules.Clear();

            EditorModule htmlStat = new EditorModule();
            htmlStat.Name = "RadEditorStatistics";
            htmlStat.Visible = true;
            htmlStat.Enabled = true;
            htmlStat.Attributes["dockingZone"] = "Bottom";
            editor.Modules.Add(htmlStat);

            if (IsAdministrator)
                editor.ImageManager.DeletePaths = aMediaPaths;
            editor.ImageManager.UploadPaths = aMediaPaths;
            editor.ImageManager.ViewPaths = aMediaPaths;
            editor.ImageManager.MaxUploadFileSize = iMaxFileSize;

            if (IsAdministrator)
                editor.DocumentManager.DeletePaths = aMediaPaths;
            editor.DocumentManager.UploadPaths = aMediaPaths;
            editor.DocumentManager.ViewPaths = aMediaPaths;
            editor.DocumentManager.MaxUploadFileSize = iMaxFileSize;

            if (IsAdministrator)
                editor.FlashManager.DeletePaths = aMediaPaths;
            editor.FlashManager.UploadPaths = aMediaPaths;
            editor.FlashManager.ViewPaths = aMediaPaths;
            editor.FlashManager.MaxUploadFileSize = iMaxFileSize;

            if (IsAdministrator)
                editor.MediaManager.DeletePaths = aMediaPaths;
            editor.MediaManager.UploadPaths = aMediaPaths;
            editor.MediaManager.ViewPaths = aMediaPaths;
            editor.MediaManager.MaxUploadFileSize = iMaxFileSize;

            if (IsAdministrator)
                editor.TemplateManager.DeletePaths = aMediaPaths;
            editor.TemplateManager.UploadPaths = aMediaPaths;
            editor.TemplateManager.ViewPaths = aMediaPaths;
            editor.TemplateManager.MaxUploadFileSize = iMaxFileSize;

            editor.NewLineMode = EditorNewLineModes.Br;
            editor.EditModes = IsAdministrator ? EditModes.All : EditModes.All ^ EditModes.Html;
        }
    }
}